
package kaizone.haining.chaobohui.fragment;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.args.GuanChaoTimeable;
import kaizone.haining.chaobohui.json.FirstReadDO;
import kaizone.haining.chaobohui.json.Intro;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.WeatherDO;
import kaizone.haining.chaobohui.json.WeatherDO.WeatherInfo;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.widget.CBHItemDropView;
import android.app.LauncherActivity.ListItem;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CBHFristReadFragment extends Fragment implements DataControl.IDataUpdateListener,
        CBHItemDropView.OnDropClickListener {

    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/remote/rest/first_read.htm";

    public static final String DATA_WEATHER = "http://m.weather.com.cn/data/101210303.html";

    private FragmentManager mFragmentManager;

    private CBHItemDropView mItemPreView;

    private CBHItemDropView mItemInfo;

    private CBHItemDropView mItemNearby;

    private CBHItemDropView mItemPrompt;

    private CBHItemDropView mItemWeather;

    private CBHItemDropView mItemContact;

    private DataControl mDataControl;

    private ImageFetcher mImageFetcher;

    public CBHFristReadFragment(FragmentManager mgr) {
        mFragmentManager = mgr;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getActivity() instanceof CBHActivity) {
            mImageFetcher = ((CBHActivity) getActivity()).getImageFetcher();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = (View) inflater.inflate(R.layout.cbh_fragment_fristread, null);
        mItemPreView = (CBHItemDropView) view.findViewById(R.id.read01);
        mItemInfo = (CBHItemDropView) view.findViewById(R.id.read02);
        mItemNearby = (CBHItemDropView) view.findViewById(R.id.read03);
        mItemPrompt = (CBHItemDropView) view.findViewById(R.id.read04);
        mItemWeather = (CBHItemDropView) view.findViewById(R.id.read05);
        mItemContact = (CBHItemDropView) view.findViewById(R.id.read06);
        initwidget();

        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);

        return view;
    }

    private void initwidget() {
        mItemPreView.setFlagText("城市导航");
        mItemPreView.setFlagImage(R.drawable.png_citylook1);
        mItemPreView.setFragmentManager(mFragmentManager);

        mItemInfo.setFlagText("潮博会介绍");
        mItemInfo.setFlagImage(R.drawable.png_cbh1);
        mItemInfo.setFragmentManager(mFragmentManager);

        mItemNearby.setFlagText("文化周边");
        mItemNearby.setFlagImage(R.drawable.png_culture1);
        mItemNearby.setFragmentManager(mFragmentManager);

        mItemPrompt.setFlagText("潮汛信息");
        mItemPrompt.setFlagImage(R.drawable.png_gczn1);
        mItemPrompt.setFragmentManager(mFragmentManager);
        GuanChaoTimeable timeable = new GuanChaoTimeable();
        mItemPrompt.setContentText(timeable.getChaoXunText());

        mItemWeather.setFlagText("海宁天气");
        mItemWeather.setFlagImage(R.drawable.png_weather1);
        mItemWeather.setFragmentManager(mFragmentManager);

        mItemContact.setFlagText("紧急服务电话");
        mItemContact.setFlagImage(R.drawable.png_anycall1);
        mItemContact.setFragmentManager(mFragmentManager);
        mItemContact.setContentText(getString(R.string.jjfwdh));

        mItemPreView.setDropClickListener(this);
        mItemInfo.setDropClickListener(this);
        mItemNearby.setDropClickListener(this);
        mItemPrompt.setDropClickListener(this);
        mItemWeather.setDropClickListener(this);
        mItemContact.setDropClickListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        mDataControl.exec(DATA_SOURCE_URL);
        mDataControl.exec(DATA_WEATHER);
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        if (requeststring.equals(DATA_SOURCE_URL)) {
            FirstReadDO firstReadDO = FirstReadDO.parseJson(datastring);
            return firstReadDO;
        }//
        else if (requeststring.equals(DATA_WEATHER)) {
            WeatherDO weatherDO = WeatherDO.parseJson(datastring);
            return weatherDO;
        }
        return null;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        //
        if (result instanceof FirstReadDO) {
            FirstReadDO firstReadDO = (FirstReadDO) result;
            if (firstReadDO.success) {
                FirstReadDO.Data data = firstReadDO.getData();
                List<Item> cultureList = data.getCultureList();
                mItemNearby.setCultureList(cultureList);
                Intro intro = data.getIntro();
                if (intro != null) {
                    String hnintro = intro.getHnIntro();
                    mItemPreView.setHnIntro(hnintro);

                    List<Item.PicModel> picmodels = intro.getHnMapPicModelList();
                    String chaoexpo = intro.getChaoExpoIntro();
                    mItemInfo.setchaoExpoIntro(chaoexpo, picmodels, mImageFetcher);

                }
            }
        }
        //
        else if (result instanceof WeatherDO) {
            WeatherDO weatherDO = (WeatherDO) result;
            WeatherInfo info = weatherDO.getData();
            if (info != null) {
                mItemWeather.setWeather(info);
            }
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    public void hideAllItem() {
        mItemPreView.hideDropContent();
        mItemInfo.hideDropContent();
        mItemNearby.hideDropContent();
        mItemPrompt.hideDropContent();
        mItemContact.hideDropContent();
        mItemWeather.hideDropContent();
    }

    @Override
    public void onStartOnClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onBeforeDropContentExtend(View v) {
        hideAllItem();
    }

    @Override
    public void onBeforeDropContentHide(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAfterDropContentExtend(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAfterDropContentHide(View v) {
        // TODO Auto-generated method stub

    }

}
